<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          size="mini"
        ></el-button>
        <!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del">
        </el-button> -->
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="search.keyword"
            placeholder="输入课程名称"
            @keydown.enter="change"
            clearable
          ></el-input>
          <el-select v-model="search.classify" placeholder="科目名称">
            <el-option
              v-for="item in selectClassify.courseclassify"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="change"
          ></el-button>
          <!-- <scFilterBar
            filterName="filterName"
            :options="options"
            @filterChange="change"
          >
            <template #default="{ filterLength, openFilter }">
              <el-badge
                :value="filterLength"
                type="danger"
                :hidden="filterLength <= 0"
              >
                <el-button
                  icon="el-icon-filter"
                  @click="openFilter"
                ></el-button>
              </el-badge>
            </template>
          </scFilterBar> -->
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :params="filterData"
        :apiObj="list.apiObj"
        row-key="id"
        @selection-change="selectionChange"
        stripe
        border
      >
        <!-- <el-table-column type="selection" width="50"></el-table-column> -->
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="科目" prop="type" width="150"></el-table-column>
        <el-table-column label="课程" prop="subType"></el-table-column>
        <el-table-column
          label="课程大小"
          prop="memory"
          width="80"
        ></el-table-column>
        <el-table-column
          label="预加载时间"
          prop="loadTime"
          width="100"
        ></el-table-column>
        <el-table-column label="操作" width="80">
          <template #default="scope">
            <el-button plain size="small" @click="table_show(scope.row.url)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <save-dialog
    v-if="dialog.save"
    ref="saveDialog"
    @success="handleSaveSuccess"
    @closed="dialog.save = false"
  >
  </save-dialog>

  <!-- 
	<el-drawer v-model="dialog.info" :size="800" title="详细" custom-class="drawerBG" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer> -->
</template>

<script>
import scFilterBar from "@/components/scFilterBar/index.vue";
import saveDialog from "./save-dialog";
// import info from './info'

export default {
  name: "courseware-list",
  components: {
    saveDialog,
    scFilterBar,
  },
  data() {
    return {
      dialog: {
        save: false,
        info: false,
      },
      list: {
        apiObj: this.$API.courseware.list,
      },
      selectClassify: {
        courseclassify: [],
      },
      selection: [],
      search: {
        keyword: "",
        classify:"",
      },
      filterData: {
        id: null,
        type: "",
        subType: "",
      },
      defaultFilter: {
        id: null,
        type: "",
        subType: "",
      },
      options: [
        {
          label: "课程",
          value: "subType",
          type: "text",
          selected: true,
          placeholder: "请输入课程",
        },
      ],
    };
  },
  mounted() {
    const res = this.$API.courseware.courseclassify.get();
    res.then(v => {
      this.selectClassify.courseclassify = v.data;
    }).catch();
  },
  methods: {
    //窗口新增
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open();
      });
    },
    //窗口编辑
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //页面新增
    addPage() {
      this.$router.push({
        path: "/template/list/crud/detail",
      });
    },
    //页面编辑
    table_edit_page(row) {
      this.$router.push({
        path: "/template/list/crud/detail",
        query: {
          id: row.id,
        },
      });
    },
    //查看
    table_show(row) {
      window.open(row, "_blank");
    },
    //删除明细
    async table_del(row, index) {
      var reqData = { id: row.id };
      var res = await this.$API.demo.post.post(reqData);
      if (res.code == 200) {
        this.$refs.table.removeIndex(index);
        this.$message.success("删除成功");
      } else {
        this.$alert(res.message, "提示", { type: "error" });
      }
    },
    //批量删除
    async batch_del() {
      var confirmRes = await this.$confirm(
        `确定删除选中的 ${this.selection.length} 项吗？`,
        "提示",
        {
          type: "warning",
          confirmButtonText: "删除",
          confirmButtonClass: "el-button--danger",
        }
      ).catch(() => {});

      if (!confirmRes) {
        return false;
      }

      var ids = this.selection.map((v) => v.id);
      this.$refs.table.removeKeys(ids);
      this.$message.success("操作成功");
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //本地更新数据
    handleSaveSuccess(data, mode) {
      //为了减少网络请求，直接变更表格内存数据
      if (mode == "add") {
        this.$refs.table.unshiftRow(data);
      } else if (mode == "edit") {
        this.$refs.table.updateKey(data);
      }

      //当然也可以暴力的直接刷新表格
      // this.$refs.table.refresh()
    },
    change(data) {
      if (Object.keys(data).length < 1) {
        // 重置filterData
        Object.assign(this.filterData, this.defaultFilter);
      }
      Object.assign(this.filterData, { subType: this.search.keyword,type:this.search.classify }, data);
      this.$refs.table.refresh();
    },
  },
};
</script>

<style></style>
